package com.zhao.dogliving.fragment.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.zhao.dogliving.R;
import com.zhao.dogliving.fragment.home.itemhomefragment.MovieFragment;
import com.zhao.dogliving.fragment.home.itemhomefragment.PlayFragment;
import com.zhao.dogliving.fragment.home.itemhomefragment.ShowFragment;
import com.zhao.dogliving.fragment.home.itemhomefragment.VarietyFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.zhao.dogliving.R.id.imageView_fragmnet_home_more;

/**
 * Created by 张昭 on 2016/12/21.
 */

public class HomeFragment extends Fragment {
    private ViewPager viewPager_Home;
    private TabLayout tabLayout_Home;
    private ImageView imageView_search,imageView_more;
    private Context mContext;
    //申明碎片集合
    private List<Fragment> mList=new ArrayList<>();
    //申明顶部导航数组
    private String[] mArrTabTitle = null;
    //申明适配器
    private HomeViewPagerAdapter adapter;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager_Home= (ViewPager) view.findViewById(R.id.viewpager_fragment_home);
        tabLayout_Home= (TabLayout) view.findViewById(R.id.tablayout_fragment_home);
        imageView_more= (ImageView) view.findViewById(imageView_fragmnet_home_more);
        imageView_search= (ImageView) view.findViewById(R.id.imageView_fragmnet_home_search);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        iniView();
    }

    private void iniView() {
        adapter=new HomeViewPagerAdapter(mList,getActivity().getSupportFragmentManager(),mArrTabTitle);
        viewPager_Home.setOffscreenPageLimit(1);
        viewPager_Home.setAdapter(adapter);
        tabLayout_Home.setupWithViewPager(viewPager_Home);
    }

    private void initData() {
        mArrTabTitle = getResources().getStringArray(R.array.HomeTab);
        ShowFragment showFragment=new ShowFragment();
        PlayFragment playFragment=new PlayFragment();
        VarietyFragment varietyFragment=new VarietyFragment();
        MovieFragment movieFragment=new MovieFragment();
        mList.add(showFragment);
        mList.add(playFragment);
        mList.add(varietyFragment);
        mList.add(movieFragment);
    }
}
